package com.betterjr.modules.application;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.web.ControllerExceptionHandler;
import com.betterjr.common.web.ControllerExceptionHandler.ExceptionHandler;
import com.betterjr.common.web.Servlets;

@Controller
@RequestMapping(value = "/Platform/Application")
public class ApplicationController {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationController.class);

    @Reference(interfaceClass = IApplicationService.class)
    private IApplicationService applicationService;

    /**
     * 
     * @Title: saveOrUpdateApplication 
     * @Description: 新增或者保存应用信息 
     * @param request
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2017年11月15日 下午4:57:13
     * 
     * id
     * applicationName
     * description
     * applicationIcon
     * applicationColor
     * applicationTypeId
     * applicationGroupId
     * ruleList
     */
    @RequestMapping(value = "/saveOrUpdateApplication", method = RequestMethod.POST)
    public @ResponseBody String saveOrUpdateApplication(final HttpServletRequest request) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return applicationService.webSaveOrUpdateApplication(anMap);
            }
        }, "操作失败", logger);
    }

    /**
     *  此方法用于查询前端根据 分组或者类型查询应用信息
     * @Title: queryEffectiveApplicationsByApplicationType 
     * @Description: 根据应用类型或者应用分类查询应用列表 ( 1 待发布 2 正常 3下架 4 删除)
     * @param anApplcationTypeId  应用类型id
     * @param anApplcationGroupId  应用分组id
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2017年11月15日 下午4:58:52
     */
    @RequestMapping(value = "/queryEffectiveApplicationsByApplicationType", method = RequestMethod.POST)
    public @ResponseBody String queryEffectiveApplicationsByApplicationType(final HttpServletRequest request,
            Long applicationTypeId, Long applicationGroupId, String businStatus) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return applicationService.webQueryEffectiveApplicationsByApplicationType(applicationTypeId,
                        applicationGroupId, businStatus);
            }
        }, "操作失败", logger);
    }

    /**
     * 
     * @Title: queryApplicationsByMap 
     * @Description: 平台admin查询应用表列表信息 
     * @param request
     * @return 平台admin查询应用表列表信息
     * @throws 
     * @author xuyp
     * @date 2017年12月12日 下午3:45:36
     * "applicationTypeId", "applicationGroupId", "ruleList",
                "applicationName", "businStatus"
     */
    @RequestMapping(value = "/queryApplicationsByMap", method = RequestMethod.POST)
    public @ResponseBody String queryApplicationsByMap(final HttpServletRequest request) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return applicationService.webQueryApplicationsByMap(anMap);
            }
        }, "操作失败", logger);
    }

    /**
     * 
     * @Title: saveModifyApplicationBusinStatus 
     * @Description: 更新应用的状态 
     * @param request
     * @param id
     * @param businStatus 应用状态 1 待发布  2 正常 3下架 4 已停用 9已删除
     * @return 更新后的结果
     * @throws 
     * @author xuyp
     * @date 2017年11月22日 上午9:53:10
     */
    @RequestMapping(value = "/saveModifyApplicationBusinStatus", method = RequestMethod.POST)
    public @ResponseBody String saveModifyApplicationBusinStatus(final HttpServletRequest request, Long id,
            String businStatus) {
        final Map<String, Object> anMap = Servlets.getParametersStartingWith(request, "");
        logger.info("入参：" + anMap);
        return ControllerExceptionHandler.exec(new ExceptionHandler() {
            @Override
            public String handle() {
                return applicationService.webSaveModifyApplicationBusinStatus(id, businStatus);
            }
        }, "操作失败", logger);
    }

}
